//
//  LGwebView.m
//  lgdw
//
//  Created by Apple on 15/12/10.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "LGwebViewController.h"
#import "LGDiscover.h"
@interface LGwebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
/** 遮罩view */
@property (nonatomic , weak) UIView *coverView;
/** 加载指示器 */
@property (nonatomic , weak) UIActivityIndicatorView *loadingView;



@end
@implementation LGwebViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    
    NSURL *url = [NSURL URLWithString:self.discover.share_url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
    self.webView.delegate = self;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - /************** 代理 **************/

- (void)webViewDidStartLoad:(UIWebView *)webView {
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor lightGrayColor];
    [coverView addSubview:self.loadingView];
    [self.loadingView startAnimating];
    self.coverView = coverView;
    
    self.webView.hidden = YES;
    self.webView.scrollView.hidden = YES;
    [self.view addSubview:self.coverView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    /** 导航  */
    
    NSString *navgationJS1 = @"document.getElementsByClassName('inner clearfix')[0].remove();";
    NSString *navgationJS = @"document.getElementsByClassName('am-g')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:navgationJS];
    [webView stringByEvaluatingJavaScriptFromString:navgationJS1];
    
    /** 广告  */
    NSString *ADJS = @"document.getElementsByClassName('am-btn am-btn-secondary')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:ADJS];
    /** 分享  */
    NSString *shareJS = @"document.getElementById('shareDiv').remove();";
    [webView stringByEvaluatingJavaScriptFromString:shareJS];
    /** 头像 */
    NSString *headJS = @"document.getElementsByClassName('author')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:headJS];
    /** 时间 */
    NSString *timeJS = @"document.getElementsByClassName('time')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:timeJS];
    /** 打开 */
    NSString *openJS = @"document.getElementsByClassName('to-app')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:openJS];
    /** 脚 */
    NSString *footJS = @"document.getElementsByClassName('footer')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:footJS];
    
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.webView.hidden = NO;
        self.webView.scrollView.hidden = NO;
        self.coverView.hidden = YES;
    });
    
    
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //    NSLog(@"%@",request.URL);
    
    return YES;
}


#pragma mark - /************** 懒加载 **************/
/** webView */
- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
        _webView.frame = self.view.bounds;
    }
    return _webView;
}
/** 加载指示器 */
- (UIActivityIndicatorView *)loadingView {
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [self.webView addSubview:loadingView];
        
        // 居中
        loadingView.center = self.view.center;
        self.loadingView = loadingView;
    }
    return _loadingView;
}
@end
